package controle;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import lejos.pc.comm.NXTComm;
import lejos.pc.comm.NXTCommException;
import lejos.pc.comm.NXTCommFactory;
import lejos.pc.comm.NXTInfo;
import vista.MainWindow;


public class BluetoothController {

	static private NXTComm nxtComm;
	private MainWindow vista;
	//Criando streams de dados para o NXT
	InputStream is;
	OutputStream os;
	
	//Criando streams de dados genericos
	DataOutputStream dos;
	DataInputStream dis;
	
	boolean conectou;
	
	public BluetoothController(MainWindow vista) {
		this.vista = vista;
	}

	public void connect(String device) throws NXTCommException {
		nxtComm = null;
		conectou = false;

		prepareDriver();
		
		NXTInfo[] nxtInfo = searchDevice(device);
		
		printToPanel("Connecting to " + nxtInfo[0].name);
		
		openConnection(nxtInfo);
		if (!conectou) {
			throw new NXTCommException("Failed to connect to device " + nxtInfo[0].name);
		}
		else
		{
			// TODO consertar aqui
			/*jBtnVelocidade.setEnabled(true);
			jBtnAndar.setEnabled(true);
			jBtnVoltar.setEnabled(true);
			jBtnGirarE.setEnabled(true);
			jBtnGirarD.setEnabled(true);			
			jBtnVirarE.setEnabled(true);
			jBtnVirarD.setEnabled(true);
			jBtnParar.setEnabled(true);
			jBtnMedir.setEnabled(true);
			jBtnConectar.setEnabled(false);*/
			printToPanel("Connected to " + nxtInfo[0].name);
			
			//Criando streams de dados para o NXT
			is = nxtComm.getInputStream();
			os = nxtComm.getOutputStream();
			
			//Criando streams de dados genericos
			dos = new DataOutputStream(os);
			dis = new DataInputStream(is);
		}
	}
	
	private void printToPanel(String arg) {
		String txt = vista.getDescriptionPanel().getText();
		vista.getDescriptionPanel().setText(txt + arg + "\n");
	}

	private void openConnection(NXTInfo[] nxtInfo) throws NXTCommException {
		try {
			conectou = nxtComm.open(nxtInfo[0]); 
		} catch (NXTCommException e) {
			throw new NXTCommException("Exception from open " + e.getMessage());
		}
	}
	
	public void closeConnection() throws IOException {
		nxtComm.close();
	}

	private NXTInfo[] searchDevice(String device) throws NXTCommException {
		NXTInfo[] nxtInfo;
		nxtInfo = null;
		try {
			nxtInfo = nxtComm.search(device,NXTCommFactory.BLUETOOTH );
		} catch (NXTCommException e1) {
			throw new NXTCommException("Bluetooth devices not found!");
		}
		return nxtInfo;
	}

	private void prepareDriver() throws NXTCommException {
		try {
			nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
		} catch (NXTCommException e) {
			throw new NXTCommException("Failed to load bluetooth driver");
		}
	}
	
	public void sendMessage(String comando) {
		try {
			dos.writeBytes(comando);
			dos.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void sendMessage(String comando, int param) {
		try {
			dos.writeBytes(comando);
			dos.writeInt(param);
			dos.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public int readInt() throws IOException {
		return dis.readInt();
	}
	
	public float readFloat() throws IOException {
		return dis.readFloat();
	}
}
